package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chainyoung.common.appcommon.entity.HdWallet;
import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.entity.HdWallets;
import com.chainyoung.common.appcommon.hdwallet.WalletStoreManger;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.imageloader.glide.transformations.BlurTransformation;
import com.chainyoung.common.utils.BlurBuilder;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.qrcodeview.QRCodeUtil;
import com.chainyoung.component_tmperson.zxing.WriterException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

public class WordsCodeActivity extends BaseActivity {


    @BindView(R2.id.iv_code)
    ImageView ivCode;
    @BindView(R2.id.iv_outside)
    ImageView ivOutside;


    private HdWalletCache hdWallet;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        hdWallet = (HdWalletCache) getIntent().getSerializableExtra("hdWallet");
        if (hdWallet != null) {
            generateQrcode();
        }
        applyBlur();

    }


    private void applyBlur() {
        ivOutside.setImageBitmap(BlurBuilder.blur(ivOutside));
        if (BlurBuilder.isBlurFlag()) {
            ivOutside.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_words_code_activity;
    }

    @OnClick(R2.id.iv_outside)
    void onClickOutSide() {
        finish();
        mContext.overridePendingTransition(
                android.view.animation.Animation.INFINITE,
                android.view.animation.Animation.INFINITE);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mContext.overridePendingTransition(
                android.view.animation.Animation.INFINITE,
                android.view.animation.Animation.INFINITE);
    }

    private void generateQrcode() {
        try {
            String codeText = hdWallet.getMnemonic();
            int logo = R.mipmap.icon_eth;
            BitmapDrawable bd = (BitmapDrawable) ContextCompat.getDrawable(mContext, logo);
            Bitmap bitmap = bd.getBitmap();
            int width = ConvertUtils.dp2px(mContext, 190);
            Bitmap bm = QRCodeUtil.createQRCode(codeText, bitmap, width, width);
            bm.setHeight(width);
            bm.setWidth(width);
            ivCode.setImageBitmap(bm);
            ivCode.setScaleType(ImageView.ScaleType.FIT_XY);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BlurBuilder.recycle();
        overridePendingTransition(android.view.animation.Animation.INFINITE,
                android.view.animation.Animation.INFINITE);
    }
}
